<?php
namespace app\Model;
use ApiSystem\DB\PdoQueryBuilder;

class adminUser extends PdoQueryBuilder
{
	protected  $table ='webchat_admin_user';

	public static $accesstoken;

	public static function getUser($userId=0)
	{
		return self::find()->where(['id'=>1])->get(1);
	}

	public static function toLogin($username='',$password='')
	{
		$userInfo = self::find()->where(['username'=>$username,'password'=>toCrypt($password)])->get(1);
		if(!$userInfo){
			return false;
		}

		self::$accesstoken = md5(serialize($userInfo).time());

		return self::find()->where(['id'=>$userInfo['id']])
						   ->update(['accesstoken'=>self::$accesstoken,'validetime'=>time()+config_item('validetime')]);//返回token
	}

	public static function getUserByToken($token='')
	{
		if((empty($token))){
			return false;
		}

		return self::find()->where(['accesstoken'=>$token,['validetime','>',time()]])->get(1);
	}
}